package com.atom.wifiupload.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * Created by IWALL on 2016/2/3.
 * 时间格式化工具类
 */
public class TimeUtils {

    /**
     * @param millis 时间戳 ms
     * @return if isToday 10:20 else if in this year 01-31 else 2015-01-31
     */
    public static String getSimpleDatetime(long millis) {
        if (millis == 0)
            return "";
        if (inSameDay(millis)) {
            return formatHourToMinute(millis);
        } else if (inSameYear(millis)) {
            return formatMonthToDay(millis);
        } else {
            return formatYearToDay(millis);
        }
    }

    /**
     * @param millis 时间戳 ms
     * @return if in this year 01-31 else 2015-01-31
     */
    public static String getSimpleDate(long millis) {
        if (millis == 0)
            return "";
        if (inSameYear(millis)) {
            return formatMonthToDay(millis);
        } else {
            return formatYearToDay(millis);
        }
    }

    //  转换为时长
    public static String getDuration(long mills) {
        long hours = TimeUnit.MILLISECONDS.toHours(mills);
        long minutes = TimeUnit.MILLISECONDS.toMinutes(mills) - TimeUnit.HOURS.toMinutes(hours);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(mills) % 60;
        if (hours == 0) {
            return String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
        } else {
            return String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);
        }
    }

    public static String formatYearToDay(long millis) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());
        return sdf.format(millis);
    }

    public static String formatYearToMonth(long millis) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月", Locale.getDefault());
        return sdf.format(millis);
    }

    public static String formatMonthToDay(long millis) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm", Locale.getDefault());
        return sdf.format(millis);
    }

    public static String formatHourToSecond(long millis) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
        return sdf.format(millis);
    }

    public static String formatHourToMinute(long millis) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
        return sdf.format(millis);
    }

    //  判断是否在同一年
    public static boolean inSameYear(long millis) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy", Locale.getDefault());
        return sdf.format(now.getTime()).equals(sdf.format(millis));
    }

    //  判断是否在同一天
    public static boolean inSameDay(long millis) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
        return sdf.format(now.getTime()).equals(sdf.format(millis));
    }

    public static String getConciseTime(long timeInMillis) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.getDefault());
        return sdf.format(timeInMillis);
    }


    public static String getSimpleTime(long timeInMillis) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        return sdf.format(timeInMillis);
    }


    public static String getCurrentDate() {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        return sdf.format(now.getTime());
    }
}
